/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.uoc.tfc.exuoc.model;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.SequenceGenerator;
import javax.persistence.GenerationType;
import javax.persistence.GeneratedValue;
import javax.persistence.Temporal;
import javax.persistence.Version;
import java.util.Date;
import edu.uoc.tfc.exuoc.config.Config;

/**
 *
 * @author emoreno
 */


@Entity
@Table(name="persones")
@Inheritance(strategy = javax.persistence.InheritanceType.JOINED)

public class Persona  implements Serializable{
    private static final long serialVersionUID = 4L;
    private Long id=Config.valorNull ;
    private String nom=Config.valorNullStr;     //character varying(30),
    private String cognoms=Config.valorNullStr; // character varying(60),
    private String adress=Config.valorNullStr;  //character varying(100),
    private String telf=Config.valorNullStr;    //character varying(20),
    private Date naixament=new Date();          //Date,
    private int versionNum=Config.valorNullInt;

    protected Persona() {
    }

    public Persona(String nom, String cognoms, String adress, String telf, Date naixament) {
        this.nom = nom;
        this.cognoms = cognoms;
        this.adress = adress;
        this.telf=telf;
        this.naixament=naixament;
    }

    public Persona(Long id, String nom, String cognoms, String adress) {
        this.id = id;
        this.nom = nom;
        this.cognoms = cognoms;
        this.adress = adress;
    }
    public Persona(Long id, String nom, String cognoms, String adress,String telf, Date naixament, int versionNum) {
        this.id = id;
        this.nom = nom;
        this.cognoms = cognoms;
        this.adress = adress;
        this.telf=telf;
        this.naixament=naixament;
        this.versionNum=versionNum;
    }

    @Column(length=100)
    public String getAdress() {
        return adress;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    @Column(length=60)
    public String getCognoms() {
        return cognoms;
    }

    public void setCognoms(String cognoms) {
        this.cognoms = cognoms;
    }

    @Id
    @SequenceGenerator(name="persones", sequenceName="seqpersones")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="persones")
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(length=30,nullable=false)
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    @Temporal(javax.persistence.TemporalType.DATE)
    public Date getNaixament() {
        return naixament;
    }

    public void setNaixament(Date naixament) {
        this.naixament = naixament;
    }

    public String getTelf() {
        return telf;
    }

    public void setTelf(String telf) {
        this.telf = telf;
    }
    
    // Control d'exceptions :StaleObjectStateException
    @Version
    @Column(name="version", nullable=true)
    public int getVersionNum() {
        return versionNum;
    }

    public void setVersionNum(int versionNum) {
        this.versionNum = versionNum;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Persona other = (Persona) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 3;
        hash = 31 * hash + (this.id != null ? this.id.hashCode() : 0);
        hash = 31 * hash + (this.nom != null ? this.nom.hashCode() : 0);
        hash = 31 * hash + (this.cognoms != null ? this.cognoms.hashCode() : 0);
        hash = 31 * hash + (this.adress != null ? this.adress.hashCode() : 0);
        return hash;
    }



}
